package cn.zhoutingrui.pethome.system.role.service.impl;

import cn.zhoutingrui.pethome.system.role.domain.Role;
import cn.zhoutingrui.pethome.system.role.mapper.RoleMapper;
import cn.zhoutingrui.pethome.system.role.service.IRoleService;
import cn.zhoutingrui.pethome.base.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ztr
 * @since 2023-02-07
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    public void add(Role role){
        roleMapper.add(role);
        addMenuPermission(role);

    }

    private void addMenuPermission(Role role) {
        List<Long> menus = role.getMenus();
        if (menus!=null){
            for (Long menu : role.getMenus()) {
                roleMapper.addRoleMenu(role.getId(),menu);
            }
        }
        List<Long> permissions = role.getPermissions();
        if (permissions!=null){
            for (Long permission : permissions) {
                roleMapper.addRolePermission(role.getId(),permission);
            }
        }
    }

    public void update(Role role){
        Long id = role.getId();
        roleMapper.deleteByRoleMenus(id);
        roleMapper.deleteByRolePermissions(id);
        addMenuPermission(role);
    }

    public void deleteRoleMenu(Long ids){
        roleMapper.deleteByRoleMenus(ids);
    }
    public void deleteRolePermission(Long ids){
        roleMapper.deleteByRolePermissions(ids);
    }

    public void delete(Long ids){
        if (ids!=null){
            deleteRoleMenu(ids);
            deleteRolePermission(ids);
        }
    }
}
